<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ page import="net.sf.json.JSONObject" %>
<%@ page import="com.jm.web.data.BaseDataEntity" %>
<%@ page import="com.jm.utils.StringHelper" %>
<%@include file="/include/common/gridbackhead.jsp"%>
<%!
private class PageAction extends BaseAction{
	//处理自定義操作
	protected boolean onCustomAction(String action) throws Exception{
		if(StringHelper.Compare("ajax_getOptions",action,true) == 0){
			onGetOptions();
		}else if(StringHelper.Compare("ajax_getAutodata",action,true) == 0){
			onGetAutodata();
		}else if(StringHelper.Compare("ajax_getMultidata",action,true) == 0){
			onGetMultidata();
		}
		return super.onCustomAction(action);
	}
	
	//取多先框的值呀
	protected void onGetMultidata() throws Exception{
		PageResult pageResult = ret.getCallResult();
		String dataSource = form.gpsv("datasource");
		String sql = form.gpsv("sql");
		String params[] = form.gpsv("params").split(",");
		String values[] = new String[params.length];
		for(int i=0;i<params.length;i++){
			values[i] = form.gpsv(params[i]);
		}
		
		if(StringHelper.IsNullOrEmpty(dataSource)  || StringHelper.IsNullOrEmpty(sql))
			ret.setRetCode(Errors.NOK);
		else
		{
			ResultSet rs = utils.executeQuery(dataSource,StringHelper.Format(sql,values));
			int colCount = 0;
			if(rs != null)
				colCount = rs.getMetaData().getColumnCount();
				
			BaseDataEntities rows = new BaseDataEntities();
			while(rs.next())
			{
				BaseDataEntity row = new BaseDataEntity();
				if(colCount == 1){
					String value = rs.getString(1);
					row.SetParamValue("text",value);
					row.SetParamValue("value",value);
					
					if(rs.getMetaData().getColumnName(1) != null && rs.getMetaData().getColumnName(1).startsWith("i18n"))
						row.SetParamValue("value",utils.i18n(value));
					
					
					
				}else if(colCount >= 2){
					String value = rs.getString(1);
					
					String text = rs.getString(2);
					if(rs.getMetaData().getColumnName(2) != null && rs.getMetaData().getColumnName(2).startsWith("i18n"))
						text = utils.i18n(text);
						
					row.SetParamValue("value",value);
					row.SetParamValue("text",text);
					if(colCount > 2){
						for(int i = 3 ;i<=colCount ;i++){
							String colName = rs.getMetaData().getColumnName(i);
							String strvalue = rs.getString(i);
							if(colName != null && colName.startsWith("i18n"))
								strvalue = utils.i18n(strvalue);
							row.SetParamValue(colName.replace("i18n",""),strvalue);
						}
					}
				}
				
				JSONObject json = new JSONObject();
				row.FillJSONObject(json);
				rows.add(row);
			}
			pageResult.getData().SetParamValue("multisdata",rows);
			
			if(StringHelper.IsNullOrEmpty(pageResult.getJSCode())){
				pageResult.setJSCode(form.gpsv("jscode"));
			}
			if(StringHelper.IsNullOrEmpty(pageResult.getJSBeforeCode())){
				pageResult.setJSBeforeCode(form.gpsv("jsbcode"));
			}
		}
		this.getPage().Output(pageResult.ToJSONString());
	}
	
	//取下combox的值呀
	protected void onGetAutodata() throws Exception{
		PageResult pageResult = ret.getCallResult();
		String dataSource = form.gpsv("datasource");
		String sql = form.gpsv("sql");
		String params[] = form.gpsv("params").split(",");
		String values[] = new String[params.length];
		for(int i=0;i<params.length;i++){
			values[i] = form.gpsv(params[i]);
		}
		
		if(StringHelper.IsNullOrEmpty(dataSource)  || StringHelper.IsNullOrEmpty(sql))
			ret.setRetCode(Errors.NOK);
		else
		{
			ResultSet rs = utils.executeQuery(dataSource,StringHelper.Format(sql,values));
			int colCount = 0;
			if(rs != null)
				colCount = rs.getMetaData().getColumnCount();
				
			BaseDataEntities rows = new BaseDataEntities();
			while(rs.next())
			{
				BaseDataEntity row = new BaseDataEntity();
				if(colCount == 1){
					String value = rs.getString(1);
					row.SetParamValue("value",value);
					row.SetParamValue("realvalue",value);
					
					if(rs.getMetaData().getColumnName(1) != null && rs.getMetaData().getColumnName(1).startsWith("i18n"))
						row.SetParamValue("realvalue",utils.i18n(value));
					
					
					
				}else if(colCount >= 2){
					String value = rs.getString(1);
					String text = rs.getString(2);
					
					if(rs.getMetaData().getColumnName(2) != null && rs.getMetaData().getColumnName(2).startsWith("i18n"))
						text = utils.i18n(text);
						
					row.SetParamValue("realvalue",value);
					row.SetParamValue("value",text);
					if(colCount > 2){
						for(int i = 3 ;i<=colCount ;i++){
							String colName = rs.getMetaData().getColumnName(i);
							String strvalue = rs.getString(i);
							if(colName != null && colName.startsWith("i18n"))
								strvalue = utils.i18n(strvalue);
							row.SetParamValue(colName.replace("i18n",""),strvalue);
						}
					}
				}
				
				JSONObject json = new JSONObject();
				row.FillJSONObject(json);
				rows.add(row);
			}
			pageResult.getData().SetParamValue("autodata",rows);
			
			if(StringHelper.IsNullOrEmpty(pageResult.getJSCode())){
				pageResult.setJSCode(form.gpsv("jscode"));
			}
			if(StringHelper.IsNullOrEmpty(pageResult.getJSBeforeCode())){
				pageResult.setJSBeforeCode(form.gpsv("jsbcode"));
			}
		}
		this.getPage().Output(pageResult.ToJSONString());
	}
	
	//取下拉选框的值呀
	protected void onGetOptions() throws Exception{
		PageResult pageResult = ret.getCallResult();
		String dataSource = form.gpsv("datasource");
		String sql = form.gpsv("sql");
		String params[] = form.gpsv("params").split(",");
		String values[] = new String[params.length];
		for(int i=0;i<params.length;i++){
			values[i] = form.gpsv(params[i]);
		}
		
		if(StringHelper.IsNullOrEmpty(dataSource)  || StringHelper.IsNullOrEmpty(sql))
			ret.setRetCode(Errors.NOK);
		else
		{
			if(form.gpbv("debug",false)){
				out("dataSource:"+dataSource);
				out("sql:"+StringHelper.Format(sql,values));
			}
			
			ResultSet rs = utils.executeQuery(dataSource,StringHelper.Format(sql,values));
			int colCount = 0;
			if(rs != null)
				colCount = rs.getMetaData().getColumnCount();
				
			BaseDataEntities rows = new BaseDataEntities();
			while(rs.next())
			{
				BaseDataEntity row = new BaseDataEntity();
				if(colCount == 1){
					String value = rs.getString(1);
					row.SetParamValue("text",value);
					row.SetParamValue("value",value);
					
					if(rs.getMetaData().getColumnName(1) != null && rs.getMetaData().getColumnName(1).startsWith("i18n"))
						row.SetParamValue("value",utils.i18n(value));
					
					
					
				}else if(colCount >= 2){
					String value = rs.getString(1);
					
					String text = rs.getString(2);
					if(rs.getMetaData().getColumnName(2) != null && rs.getMetaData().getColumnName(2).startsWith("i18n"))
						text = utils.i18n(text);
						
					row.SetParamValue("value",value);
					row.SetParamValue("text",text);
					if(colCount > 2){
						for(int i = 3 ;i<=colCount ;i++){
							String colName = rs.getMetaData().getColumnName(i);
							String strvalue = rs.getString(i);
							if(colName != null && colName.startsWith("i18n"))
								strvalue = utils.i18n(strvalue);
							row.SetParamValue(colName.replace("i18n",""),strvalue);
						}
					}
				}
				
				JSONObject json = new JSONObject();
				row.FillJSONObject(json);
				rows.add(row);
			}
			pageResult.getData().SetParamValue("options",rows);
			if(StringHelper.IsNullOrEmpty(pageResult.getJSCode())){
				pageResult.setJSCode(form.gpsv("jscode"));
			}
			if(StringHelper.IsNullOrEmpty(pageResult.getJSBeforeCode())){
				pageResult.setJSBeforeCode(form.gpsv("jsbcode"));
			}
		}
		this.getPage().Output(pageResult.ToJSONString());
	}
}
%>
<%@include file="/include/common/gridbackend.jsp"%>